package com.hexinfo.dmpro.dev.data.transfer.model;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.hexinfo.dmpro.dev.data.transfer.common.enums.DevDataExportModelEnum;
import com.hexinfo.dmpro.dev.data.transfer.common.enums.DevDataTransferExportTaskStatusEnum;
import io.swagger.annotations.ApiModel;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import lombok.experimental.Accessors;
import org.springframework.validation.annotation.Validated;

/**
 * 总导出任务表
 *
 * @author yyh.huang
 * @date 2023-10-25 19:16:06
 */
@Data
@Validated
@TableName("TD_DEV_DATA_EXPORT_TASK")
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@ApiModel(value = "总导出任务表")
public class DevDataExportTask extends Model<DevDataExportTask> {
    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @TableId(type = IdType.ASSIGN_UUID)
    @ApiModelProperty(value = "ID")
    private String id;
    /**
     * 任务名
     */
    @ApiModelProperty(value = "任务名")
    private String taskName;
    /**
     * 以秒级时间戳为任务ID
     */
    @ApiModelProperty(value = "以秒级时间戳为任务ID")
    private String taskCode;

    /**
     * 导出模式（按源导出、按库导出、按表导出、自定义导出）
     */
    @ApiModelProperty(value = "导出模式")
    private String exportModel;
    /**
     * 导出时间段
     */
    @ApiModelProperty(value = "导出时间段")
    private String exportTimeInterval;
    /**
     * 导出总数
     */
    @ApiModelProperty(value = "导出总数")
    private Integer exportTotal;
    /**
     * 导出成功数量
     */
    @ApiModelProperty(value = "导出成功数量")
    private Integer exportSuccess;
    /**
     * 导出失败数量
     */
    @ApiModelProperty(value = "导出失败数量")
    private Integer exportFail;
    /**
     * FTP存储空间大小
     */
    @ApiModelProperty(value = "FTP存储空间大小")
    private String ftpSize;
    /**
     * 导出文件大小
     */
    @ApiModelProperty(value = "导出文件大小")
    private Double fileSize;
    /**
     * 导出文件存储位置
     */
    @ApiModelProperty(value = "导出文件存储位置")
    private String filePath;
    /**
     * 任务状态
     */
    @ApiModelProperty(value = "任务状态")
    private String taskStatus;
    /**
     * 任务开始时间
     */
    @ApiModelProperty(value = "任务开始时间")
    private Date startTime;
    /**
     * 任务结束时间
     */
    @ApiModelProperty(value = "任务结束时间")
    private Date endTime;

    @ApiModelProperty(value = "导出类型, 全量表、增量表")
    private String exportType;

    @ApiModelProperty(value = "任务创建失败日志")
    private String errorMessage;

    /**
     * 创建人
     */
    @ApiModelProperty(value = "创建人")
    @TableField(fill = FieldFill.INSERT)
    private String createBy;
    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date createTime;
    /**
     * 更新人
     */
    @ApiModelProperty(value = "更新人")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String lastUpdateBy;
    /**
     * 更新时间
     */
    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date lastUpdateTime;
    /**
     * 删除标识(0 正常，1 删除数据文件且不展示， 2删除数据文件但展示)
     */
    @ApiModelProperty(value = "删除标识")
    @TableField(fill = FieldFill.INSERT)
    private String del;

    @ApiModelProperty(value = "是否删除数据文件")
    private String isDelDataFile;

    @TableField(exist = false)
    private String customTableListPath;

    @TableField(exist = false)
    private String sourceName;

    @ApiModelProperty(value = "导出库名")
    private String databaseName;

    @TableField(exist = false)
    private List<String> tableNameList;

    @TableField(exist = false)
    private List<String> dateList;

    @TableField(exist = false)
    private String quickDate;

    @TableField(exist = false)
    private Integer buildCount;

    public List<String> getDateList() {
        if (StrUtil.isBlank(quickDate)) {
            return dateList;
        } else {
            Date nowDate = DateUtil.date();
            String endTime = DateUtil.today();
            switch (quickDate) {
                case "lastWeek":
                    return Arrays.asList(DateUtil.formatDate(DateUtil.offsetDay(nowDate, -7)), endTime);
                case "lastMonth":
                    return Arrays.asList(DateUtil.formatDate(DateUtil.offsetDay(nowDate, -30)), endTime);
                case "lastSixMonths":
                    return Arrays.asList(DateUtil.formatDate(DateUtil.offsetMonth(nowDate, -6)), endTime);
                case "lastYear":
                    return Arrays.asList(DateUtil.formatDate(DateUtil.offsetMonth(nowDate, -12)), endTime);
                case "lastTwoYears":
                    return Arrays.asList(DateUtil.formatDate(DateUtil.offsetMonth(nowDate, -24)), endTime);
                case "custom":
                    return dateList;
                default:
                    return Arrays.asList(DateUtil.formatDate(DateUtil.offsetDay(nowDate, -30)), endTime);
            }
        }
    }

    public String getTaskStatusName() {
        if (StrUtil.isNotBlank(taskStatus)) {
            return DevDataTransferExportTaskStatusEnum.valueOf(taskStatus).getName();
        }
        return "";
    }

    public String getStartTimeString() {
        if (startTime != null) {
            return DateUtil.formatDateTime(startTime);
        }
        return "";
    }

    public String getEndTimeString() {
        if (endTime != null) {
            return DateUtil.formatDateTime(endTime);
        }
        return "";
    }

    public String getTimeConsuming() {
        if (startTime != null && endTime != null) {
            long seconds = DateUtil.between(startTime, endTime, DateUnit.SECOND);
            if (seconds < 60) {
                return seconds + "秒";
            } else {
                return StrUtil.format("{}分{}秒", seconds / 60, seconds % 60);
            }
        }
        return "-";
    }

    public String getIsDelDataFileString() {
        switch (StrUtil.isNotEmpty(isDelDataFile) ? isDelDataFile : "") {
            case "Y":
                return "已删除";
            case "H":
                return "删除中";
            case "N":
                return "未删除";
            default:
                return "";
        }
    }

    public String getFileSizeString() {
        double size = fileSize == null ? 0 : fileSize;
        if (size < 1024) {
            return size + " Byte";
        } else if (size < 1024 * 1024){
            return NumberUtil.round(size / 1024.0, 2).toString() + " KB";
        } else if (size < 1024 * 1024 * 1024) {
            return NumberUtil.round(size / (1024.0 * 1024.0), 2).toString() + " MB";
        } else {
            return NumberUtil.round(size / (1024.0 * 1024.0 * 1024), 2).toString() + " GB";
        }
    }

    public String getExportModelString(){
        return StrUtil.format("【{}】", DevDataExportModelEnum.valueOf(exportModel).getName())
                + (StrUtil.equals(exportModel, "database") ? (StrUtil.equals(exportType, "complete") ? "【全量】" : (StrUtil.equals(exportType, "increment") ? "【增量】" : "")) : "")
                + (StrUtil.isNotEmpty(databaseName) ? StrUtil.format("【{}】", databaseName) : "")
                + (StrUtil.equals(exportType, "complete") ? "" : StrUtil.format("【{}】", exportTimeInterval));
    }
}
